﻿@* @inject IMaterialService MaterialService

<MudDialog>
    <DialogContent>
        <MudForm @ref="_editRequestForm" Model="_editRequestDto">
            <MudGrid>
                <MudItem xs="12" md="6">
                    <MudAutocomplete T="MaterialInfoResponseDto"
                                     Label="物料编码" @bind-Value="_editRequestDto.Material"
                                     SearchFunc="@MaterialSearch" ToStringFunc="@(e => e?.Code)"
                                     Dense="true" Margin="Margin.Dense" Variant="Variant.Outlined"/>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudTextField @bind-Value="_editRequestDto.Material.Name" Label="物料名称"
                                  Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudTextField @bind-Value="_editRequestDto.Material.Specification" Label="规格"
                                  Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudTextField @bind-Value="_editRequestDto.Material.Unit" Label="单位"
                                  Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudNumericField @bind-Value="_editRequestDto.Quality" For="@(() => _editRequestDto.Quality)" Label="数量"
                                     Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudNumericField>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudNumericField @bind-Value="_editRequestDto.LossRate" For="@(() => _editRequestDto.LossRate)" Label="损耗率"
                                     Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudNumericField>
                </MudItem>
            </MudGrid>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="SubmitHandle">保存</MudButton>
        <MudButton Variant="Variant.Filled" OnClick="CancelHandle">取消</MudButton>
    </DialogActions>
</MudDialog>

@code {
    private StandardBomDetailEditRequestDto _editRequestDto = new();

    private MudForm _editRequestForm = new();

    [CascadingParameter]
    MudDialogInstance? MudDialog { get; set; }

    [Parameter]
    public StandardBomDetailEditRequestDto? Model { get; set; }

    private async Task<IEnumerable<MaterialInfoResponseDto>> MaterialSearch(string value)
    {
        var result = await MaterialService.QueryMaterials(new MaterialSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            return result.Data!;
        }
        return new List<MaterialInfoResponseDto>();
    }

    void CancelHandle()
    {
        MudDialog?.Cancel();
    }

    void SubmitHandle()
    {
        MudDialog?.Close(DialogResult.Ok(_editRequestDto));
    }

    protected override async Task OnInitializedAsync()
    {
        if (Model != null) _editRequestDto = Model;
        await base.OnInitializedAsync();
    }

} *@